package com.example.administrator.touchtest;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Administrator on 2016/12/1.
 */

public class TouchView extends View implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
    private Matrix matrix;
    private Bitmap bitmap;
    private float lastDis;
    private float lastDegress;
    private GestureDetector detector;
    private boolean isDouble;

    public TouchView(Context context) {
        this(context,null);
    }

    public TouchView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TouchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.img);
        matrix = new Matrix();  //matrix  矩阵
        detector = new GestureDetector(context,this);// 手势监听的类
        detector.setOnDoubleTapListener(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap,matrix,null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //判断是否两指触摸
        if (event.getPointerCount() == 2) {
            isDouble = true;
            //求出中心点的坐标（cx，xy）
            float x1 = event.getX(0);
            float x2 = event.getX(1);
            float y1 = event.getY(0);
            float y2 = event.getY(1);
            float cx = (x1 + x2) / 2;
            float cy = (y1 + y2) / 2;
            float dis = (float) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));//缩放
            float degress = (float) Math.toDegrees(Math.atan2(y2 - y1, x2 - x1));      //旋转
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_POINTER_DOWN:
                case MotionEvent.ACTION_DOWN:
                    lastDis = dis;
                    lastDegress = degress;
                    break;
                case MotionEvent.ACTION_MOVE:
                    matrix.postScale(dis / lastDis,dis/lastDis,cx,cy);//缩放
                    matrix.postRotate(degress - lastDegress,cx,cy);   //旋转
                    lastDis = dis;
                    lastDegress = degress;
                    break;
            }
        }else if(event.getPointerCount() == 1){
            if (event.getAction() == MotionEvent.ACTION_DOWN){
                isDouble = false;
            }
            if (!isDouble){
                return detector.onTouchEvent(event);
            }
        }
        invalidate();  //提醒刷新
        return true;
    }

    /**
     * detector = new GestureDetector(context,this);// 手势监听的类
     * */
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override  //等同于click 单击事件
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override  //滚动滑动事件
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        matrix.postTranslate(-distanceX,-distanceY);  //点击拖动
        invalidate();
        return false;
    }

    @Override  //长按事件
    public void onLongPress(MotionEvent e) {

    }

    @Override  //抛动事件
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }


    /**
     * detector.setOnDoubleTapListener(this); 双击
     * */
    @Override  //单击确认
    public boolean onSingleTapConfirmed(MotionEvent e) {
        return false;
    }

    @Override  //双击  第二次按下
    public boolean onDoubleTap(MotionEvent e) {
        return false;
    }

    @Override   //双击事件   第二次按下之后抬起
    public boolean onDoubleTapEvent(MotionEvent e) {
        matrix.postScale(1.2f,1.2f,e.getX(),e.getY()); //双击放大
        invalidate();
        return false;
    }
}
